package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.SalesDao;
import cn.gson.boot.model.dao.SolutionDao;
import cn.gson.boot.model.dao.StageDao;
import cn.gson.boot.model.mapper.SolutionMapper;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class SolutionService {
    @Autowired
    SolutionMapper solutionMapper;
    @Autowired
    SolutionDao solutionDao;
    @Autowired
    SalesDao salesDao;
    @Autowired
    StageDao stageDao;

    //查询所有解决方案分页
    public List<Solution> selectSolution(String search,String empName) {
        return solutionMapper.selectSolution(search,empName);
    }

    //新增解决方案
    public void addSolution(Solution solution) {
        String Nowtime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());//一个string类型的当前时间
        java.sql.Date mysqldate = java.sql.Date.valueOf(Nowtime);
        solution.setSolutionSc(0);
        Sales sales = salesDao.findById(solution.getSales().getSalesId()).get();
        Stage stage = new Stage();
        stage.setStageId(3);
        sales.setStage(stage);
        Stage stage1 = stageDao.findById(stage.getStageId()).get();
        int i = Integer.parseInt(sales.getSalesPossibility().substring(0,2));
        if(solutionMapper.selectSalesSolution(solution.getSales().getSalesId()).size()==0){
            if(i>=80){
                sales.setSalseLog("   " + sales.getSalseLog()+ "日期:"+mysqldate+"\t"+"阶段:"+stage1.getStageName());
                salesDao.save(sales);
            }else{
                i = i+20;
                sales.setSalesPossibility(i+"%");
                sales.setSalseLog("   " +sales.getSalseLog() + "日期:"+mysqldate+"\t"+"阶段:"+stage1.getStageName());
                salesDao.save(sales);
            }
        }else{
            sales.setSalseLog("\t"+ sales.getSalseLog() + "日期:"+mysqldate+"\t"+"阶段:"+stage1.getStageName());
            salesDao.save(sales);
        }
        solutionDao.save(solution);
    }

    //查询需要修改的解决方案
    public List<Solution> upSolution(Integer solutionId) {
        return solutionMapper.selectSolutions(solutionId);
    }

    //删除解决方案
    public void delSolution(Integer solutionId) {
        Solution solution = solutionDao.findById(solutionId).get();
        if(solutionMapper.selectSalesSolution(solution.getSales().getSalesId()).size()==1){
            Sales sales = salesDao.findById(solution.getSales().getSalesId()).get();
            int i = Integer.parseInt(sales.getSalesPossibility().substring(0,2));
            if(i>=60){
                i = i-20;
                sales.setSalesPossibility(i+"%");
            }
            salesDao.save(sales);
            solution.setSolutionSc(1);
            solutionDao.save(solution);
        }else {
            solution.setSolutionSc(1);
            solutionDao.save(solution);
        }
    }

    //查询客户列表
    public List<Kehu> selectKh(String khsearch,String empName) {
        return solutionMapper.selectKehu(khsearch,empName);
    }

    //对应机会
    public List<Sales> selectSales(String khName) {
        return solutionMapper.selectSales(khName);
    }

    //查询对应需求
    public List<Demand> selectDemand(Integer salesId) {
        return solutionMapper.selectDemand(salesId);
    }

    //对应竞争对手
    public List<Competitor> selectCompetitor(Integer salesId) {
        return solutionMapper.selectCompetitor(salesId);
    }

    //销售机会页面已生成解决方案不可删除
    public List<Solution> selectSalesSolution(Integer salesId) {
        return solutionMapper.selectSalesSolution(salesId);
    }
}
